<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of login
 *
 * @author zwang
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class login extends User_Controller {

    //put your code here
    function __construct() {
        parent::__construct();
        $this->load->model("login_model");
    }

    public function index() {
        $is_logged_in = $this->session->userdata('is_logged_in');
        if ($is_logged_in) {
            // $user_data = $this->session->userdata('validated');
            // $this->page_redirect($user_data['type']);
            $this->session->unset_userdata('validated');
            $this->session->unset_userdata('is_logged_in');
            $this->session->unset_userdata();
            $this->session->sess_destroy();
        }
        $this->load->view('login');
    }

    function quickLogin() {
        $page = $this->input->get_post('page');
        $this->load->view('quick_login', array('page' => $page));
    }

    function verify_login() {
        $page = $this->input->get_post('page');
        $username = $this->input->get_post('username');
        $password = $this->input->get_post('password');
        $this->session->set_userdata('username', $username);

        $result = $this->login_model->validate($username, $password);

        if ($result == "SUCCESS") {
            $userid = $this->get_userID($username);
            $fullname = $this->get_userFullName($userid);
            $email = $this->get_userEmail($userid);
            $usertype = $this->get_userType($username);
            $this->session->set_userdata('userid', $userid);
            $this->session->set_userdata('fullname', $fullname);
            $this->session->set_userdata('email', $email);
            $this->session->set_userdata('userType', $usertype);
            $this->session->set_userdata('is_logged_in', TRUE);
            $this->session->set_userdata('validated', TRUE);
            $user_type = $this->get_userType($username);
            if ($page) {
                echo "success";
                return;
            }
            switch ($user_type) {
                case $this->type_user:
                    redirect('user');
                    break;
                case $this->type_admin:
                    redirect('admin');
                    break;
                case $this->type_committer:
                    redirect('committer');
                    break;
                default:
                    redirect('citportal');
                    break;
            }
            return true;
        }
        $this->session->set_flashdata('fail', $result);
        redirect("login");
        return FALSE;
    }

    function forgetPassword() {
        $this->load->view('reset_password');
    }

    function resetPassword() {
        $email = $this->input->get_post('email');
        $this->session->set_flashdata('email', $email);
        if ($this->checkEmail($email)) {
            $temporary_password = $this->randomWord();
            $this->load->model('database');
            $user = $this->database->getUserByEmail($email);
            $username = $this->get_usernameByUserid($user->userid);
            $password = md5($temporary_password);
            if ($this->database->changePassword($user->userid, $password)) {
                $subject = "Citportal: reset password";
                $message = 'Username: ' . $username . "\n";
                $message .= 'Temporary Password: ' . $temporary_password . "\n";
                $message .= 'If you still have problem, please contact administrator.';
                $message .= "\n\n\nPlease don't reply this email, we won't receive your email.";
                $this->sendEmail($email, "Citportal", $message, $subject);
                $success = "Temporary Password has been sent to your email address, please check your email.";
                $this->session->set_flashdata('success', $success);
                redirect('login/forgetPassword');
                return true;
            } else {
                $error = 'Fail to change your password, please try again or contact administrator.';
            }
        } else {
            $error = 'This email not exists.';
        }
        $this->session->set_flashdata('error', $error);
        redirect('login/forgetPassword');
        echo $email;
    }

    function logout() {
        $this->session->unset_userdata('validated');
        $this->session->unset_userdata('is_logged_in');
        session_unset();
        session_destroy();
        redirect('citportal');
    }

}

?>
